package com.ww.yaodao.controller;

import cn.hutool.json.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.sun.istack.NotNull;
import com.ww.yaodao.annotation.RepeatAnnotation;
import com.ww.yaodao.config.AlipayConfig;
import com.ww.yaodao.model.bo.AliPayDO;
import com.ww.yaodao.model.bo.PayDO;
import com.ww.yaodao.model.vo.pay.PayListRespVO;
import com.ww.yaodao.model.vo.pay.PayVO;
import com.ww.yaodao.service.PayService;
import com.ww.yaodao.util.BaseResult;
import com.ww.yaodao.util.ResultEnums;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.alipay.easysdk.factory.Factory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 支付模块
 */
@RestController
@RequestMapping("/alipay")
@Api(tags = {"支付模块"})
@Validated
@Slf4j
@RequiredArgsConstructor
public class AliPayController {

    private final PayService payService;

    @GetMapping("/pay")
    @ApiOperation(value = "支付宝支付")
    @ApiResponses(
            @ApiResponse(code = 200, message = "支付成功")
    )
    public PayVO pay(@NotNull PayVO aliPayVO, HttpServletResponse httpResponse, HttpServletRequest req) throws Exception {
        log.info("进入支付宝支付.....");
        return payService.insertPayInfos(aliPayVO, httpResponse, req);
    }

    /**
     * 支付模块 - 系统使用截止日期
     *
     * @return 截止日期
     */
    @GetMapping("/getPay")
    @ApiOperation("截止日期")
    public PayListRespVO getCurrentPayInfo() {
        log.info("进入截止日期查询...");
        return payService.getCurrentInfo();
    }

    /**
     * 支付宝回调接口
     *
     * @param request
     * @return
     * @throws Exception
     */
    @GetMapping("/notify")  // 注意这里必须是POST接口
    public String payNotify(HttpServletRequest request) throws Exception {
        String url = payService.notifyInfo(request);
        return "success";
    }

}
